//
//  ShowAlertView.m
//  YXLAlertView
//
//  Created by yanxinlong on 16/1/13.
//  Copyright © 2016年 yanxinlong. All rights reserved.
//
/**
 *  屏幕宽度
 */
#define screenWidth [[UIScreen mainScreen] bounds].size.width

/**
 *  屏幕高度
 */
#define screenHeight [[UIScreen mainScreen] bounds].size.height

typedef enum {
    warningViewType = 1,
    selectViewType = 2,
    textViewType = 3,
    sureButtonTag = 101,
    cancleButtonTag = 102
}ButtonTagAndViewType;

#import "ShowAlertView.h"
@interface ShowAlertView ()<UITextFieldDelegate>
{
    UITextField *message;
}
@property (nonatomic, assign) NSInteger alertType;

@property (nonatomic, strong) NSString * alertTitle;
@property (nonatomic, strong) NSString * alertContent;
@property (nonatomic, strong) NSString * placeholderString;
@end

@implementation ShowAlertView

- (instancetype)initWithAlertString:(NSString *)alertString {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
        self.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        _alertType = warningViewType;
        _alertContent = alertString;
        [self creatUI];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)titleString andMessage:(NSString *)messages {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        _alertType = selectViewType;
        _alertTitle = titleString;
        _alertContent = messages;
       [self creatUI];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)titleString andContentPlaceholder:(NSString *)placeholder {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        _alertType = textViewType;
        _alertTitle = titleString;
        _placeholderString = placeholder;
       [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(30, screenHeight*5/18, screenWidth - 60, screenHeight/3)];
    
    backgroundView.layer.cornerRadius = 5;
    backgroundView.clipsToBounds = YES;
    backgroundView.backgroundColor = [UIColor colorWithRed:0.858 green:0.858 blue:0.858 alpha:1];
    [self addSubview:backgroundView];
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, backgroundView.frame.size.width, screenHeight/3/4)];
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:18];
    [backgroundView addSubview:titleLabel];

    //内容
    UILabel *tips = [[UILabel alloc]initWithFrame:CGRectMake(0, screenHeight/3/4, backgroundView.frame.size.width, screenHeight/3/2)];
    tips.numberOfLines = 0;
    tips.text = _alertContent;
    tips.textAlignment = NSTextAlignmentCenter;
    tips.font = [UIFont systemFontOfSize:16];
    
    //自己填写的
    message = [[UITextField alloc]initWithFrame:CGRectMake(10, screenHeight/3/4 + 10, backgroundView.frame.size.width - 20, screenHeight/12)];
    message.delegate = self;
    message.tag = 10;
    message.borderStyle = UITextBorderStyleRoundedRect;
    message.placeholder = _placeholderString;
    [message setTextColor:[UIColor lightGrayColor]];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(tips.frame)+1, backgroundView.frame.size.width, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [backgroundView addSubview:line];
    
    UIView *secondline = [[UIView alloc]initWithFrame:CGRectMake(backgroundView.frame.size.width/2-0.5, CGRectGetMaxY(line.frame), 1, screenHeight/3/4)];
    secondline.backgroundColor = [UIColor lightGrayColor];
    [backgroundView addSubview:secondline];
    
    //按钮
    UIButton *sureButton = [[UIButton alloc]init];
    sureButton.tag = sureButtonTag;
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor colorWithRed:44.0/255 green:110.0/255 blue:251.0/255 alpha:1] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:sureButton];
    
    
    UIButton *cancleButton = [[UIButton alloc]init];
    cancleButton.tag = cancleButtonTag;
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancleButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancleButton setTitleColor:[UIColor colorWithRed:44.0/255 green:110.0/255 blue:251.0/255 alpha:1] forState:UIControlStateNormal];

    
    if (_alertType == 1) {
        titleLabel.text = @"提示";
        [backgroundView addSubview:tips];
        sureButton.frame = CGRectMake(0, CGRectGetMaxY(tips.frame), backgroundView.frame.size.width, screenHeight/12);
        secondline.hidden = YES;
    }
    else if (_alertType == 2) {
        titleLabel.text = _alertTitle;
        [backgroundView addSubview:tips];
        
        sureButton.frame = CGRectMake(0, CGRectGetMaxY(tips.frame), backgroundView.frame.size.width/2, screenHeight/12);
        cancleButton.frame = CGRectMake( CGRectGetMaxX(sureButton.frame), CGRectGetMaxY(tips.frame), backgroundView.frame.size.width/2, screenHeight/12);
        [backgroundView addSubview:cancleButton];
    }
    else if (_alertType == 3) {
        
        titleLabel.text = _alertTitle;
        [backgroundView addSubview:message];
        
        sureButton.frame = CGRectMake(0, CGRectGetMaxY(tips.frame), backgroundView.frame.size.width/2, screenHeight/12);
        cancleButton.frame = CGRectMake( CGRectGetMaxX(sureButton.frame), CGRectGetMaxY(tips.frame), backgroundView.frame.size.width/2, screenHeight/12);
        [backgroundView addSubview:cancleButton];
        
    }
    
}

- (void)buttonClick:(UIButton *)sender {
    [self removeFromSuperview];
    if (sender.tag == sureButtonTag) {
        if (_alertType == selectViewType) {
            if (self.sureBlock) {
                self.sureBlock();
            }
        }else if (_alertType == textViewType) {
            if (self.sendBlock) {
                self.sendBlock(message.text);
            }
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [message resignFirstResponder];
}

@end
